// pages/auth/auth.js

import { request } from "../../request/req.js";
import regeneratorRuntime from "../../lib/runtime/runtime.js";
import { login  } from "../../utils/asyncWx.js";

Page({
  // 获取用户信息
  async handleGetUserInfo(e) {
    try {
      // 获取用户信息
      const { encryptedData, rawData, iv, signature } = e.detail;
      // 获取小程序登录成功后的code值
      const { code } = await login();
      const loginParams = { encryptedData, rawData, iv, signature, code };
      // console.log(code);
      // 发送请求获取token
      const { res } = await request({ url: "/users/wxlogin", data: loginParams, methods: "post" });
      console.log(res);
      // 吧token存储到缓存中 同时跳转回上一个页面
      wx.setStorageSync("token", token);
      wx.navigateBack({
        delta: 1,
      });
    } catch(error) {
      console.log(error);
    }
  }
})